<?php
class NoticesController extends AppController{

	var $uses = array('Notice','Company');

	/**
	 * (non-PHPdoc)
	 * @see AppController::beforeFilter()
	 */
	function beforeFilter(){
		parent::beforeFilter();
		$this->set('scriptContentInit','smat.initNoticeIndex();');
	}

	/**
	 *
	 * Enter description here ...
	 */
	public function index(){
		$this->Session->write('navigation.active',array(
				'Meldungen Verwalten'
			)
		);
		
		$companyId = 0;
		$dataIsEmpty = true;
		$this->set('dataIsEmpty',$dataIsEmpty);
		if(isset($this->params['url']['company_id']) && is_numeric($this->params['url']['company_id'])){
			$dataIsEmpty = false;
			$companyId = $this->params['url']['company_id'];
			if($companyId > 0){
				$params = array(
					'conditions'=> array('Notice.company_id'=>$companyId),
					'order' => array('Notice.record_date DESC')
				);
				$this->data = $this->Notice->find('all',$params);
			}
		}else{
			$companyId = 0;
		}
		
		
		$this->set('companyId',$companyId);
			
		// get the companies list ---------------

		$params = array (
			'fields' => array('Company.id','Company.company_name'),
		);
		$companies = $this->Company->find('list',$params);
		if($dataIsEmpty){
			$companies2[0] = 'Bitte Auswählen';
			$companies = array_merge($companies2,$companies);
		}
		$this->set('companies',$companies);
		
	}

	/**
	 *
	 * Enter description here ...
	 */
	function add(){
		$thisDate = date('Y-m-d H:i:s',time());
		if(!empty($this->data)){
			$this->Notice->set('record_date',$thisDate);
			$ok = $this->Notice->save($this->data);
			$trueMessage = 'Die Meldung wurde angelegt';
			$falseMessage = 'Die Meldung konnte nicht angelegt werden.'.$this->Session->error();
			parent::doFlash($ok, $trueMessage, $falseMessage);
		}
		// get the states list ---------------
		$params = array (
			'fields' => array('State.id','State.state_name')
		);
		$states = $this->Notice->State->find('list',$params);
		$this->set('states',$states);

		// get the companies list ---------------
		$params = array (
			'fields' => array('Company.id','Company.company_name')
		);
		$companies = $this->Notice->Company->find('list',$params);
		$this->set('companies',$companies);

		// get the StudyBranch list ---------------
		$params = array (
			'fields' => array('StudyBranch.id','StudyBranch.branch_name')
		);
		$courses = $this->Notice->StudyBranch->find('list',$params);
		$this->set('studyBranches',$courses);

		$this->data['Notice']['record_date'] = $thisDate;
	}

	/**
	 *
	 * Enter description here ...
	 */
	function update(){


	}

}
?>
